package com.seal.commons.utils.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * excel的操作工具类
 *
 * @author braveblackc
 * @since 2022/10/11 21:04
 */
public class ExcelUtils {

    /**
     * 读取excel
     * @param file excel文件
     * @return 返回结果列表
     */
    public static <T> List<T> read(File file){
        List<T> contents = new ArrayList<>();
        EasyExcel.read(file).sheet().registerReadListener(new AnalysisEventListener<T>() {
            @Override
            public void invoke(T o, AnalysisContext analysisContext) {
                contents.add(o);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).doRead();
        return contents;
    }



    /**
     * excel 文件读取异步处理
     * @param file excel文件
     * @param consumer 数据处理类
     */
    public static <T> void read(File file, Consumer<T> consumer){
        EasyExcel.read(file).sheet().registerReadListener(new AnalysisEventListener<T>() {
            @Override
            public void invoke(T o, AnalysisContext analysisContext) {
                consumer.accept(o);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).doRead();
    }

    //todo 将
    public static <T> void write(List<T> datas, HttpServletResponse response){

    }




}
